<?php
namespace app\api\controller;

use app\BaseController;
use app\api\model\MemberModel as MemberModel;
use app\api\model\SmsModel;
use think\Db;

class Member extends BaseController {

    public function index2()
    {
    		$memberModel = new MemberModel();
		//$memberModel->ddd();
		$memberModel->openid = "fdfdfdfd";
		
		if ($memberModel->save()) {
            return "辅导辅导费的test";
        } else {
            return $memberModel->getError();
        }
		
    }
	
	/**
	 * 用户登陆
	 */
	function login()
	{
		if(!$this->checkSignatrue()){return;}//检测签名
		
    		$memberModel = new MemberModel();
		
		$accountOrPhone	= $this->getParam("account");
		$password 		= $this->getParam("password");
		
		$memberInstance = $memberModel->loginByAccountPhoneWithPass($accountOrPhone,$password);

		if(!empty($memberInstance)){
			//生成userToken
			$memberId = $memberInstance["id"];
			
			$userToken = $this->generalToken($memberId,$accountOrPhone, $password);
			$userToken["memberId"] = $memberId;
			
			$memberModel->updateUserToken($userToken);	//更新用户的token	
			
			echo json_encode(array("success"=>1,"content"=>_unsetNull($memberInstance),"userToken"=>$userToken,"code"=>200,"msg"=>"ok"),true);
		}else{
			echo json_encode(array("success"=>0,"code"=>1010,"msg"=>"账号或者密码错误"),true);
		}
	}	
	
	
	/**
	 * 短信发送接口
	 */
	function sendCheckMessage()
	{
		$dd = eeGetDateTime();
		if(!$this->checkSignatrue()){return;}//检测签名

		$phone = $this->getParam("phone");
		if(!$this->pvalidate(array("phone"=>$phone),["phone"],["req"],["手机"])){//校验
			return;
		}

    		$smsModel = new SmsModel();
		$isok = $smsModel->send($phone);
		
		if($isok){
			echo json_encode(array("success"=>1,"content"=>_unsetNull($phone),"code"=>200,"msg"=>"发送短信成功"),true);
		}else{
			echo json_encode(array("success"=>0,"code"=>1011,"msg"=>"短信发送失败"),true);
		}
	}	
	
	/**
	 * 注册账号
	 */
	function register()
	{
		if(!$this->checkSignatrue()){return;}//检测签名
		
		$phone 		 = $this->getParam("phone");
		$password	 = $this->getParam("password");
		$checkCode	 = $this->getParam("checkCode");
		$openid		 = $this->getParam("openid");
		
		$nickName	 = $this->getParam("nickName");
		$realName	 = $this->getParam("realName");
		$sex		 	 = $this->getParam("sex");
		$avatar		 = $this->getParam("avatar");
		$province	 = $this->getParam("province");
		$city		 = $this->getParam("city");
		
					
		$instance = array("phone"=>$phone,"password"=>$password,"checkCode"=>$checkCode,"openid"=>$openid,
				"nickName"=>$nickName,"realName"=>$realName,"sex"=>$sex,"avatar"=>$avatar,"city"=>$city);
		if(!$this->pvalidate($instance,["phone","password","checkCode"],["req","req","req"],["手机","密码","验证码"])){//校验
			return;
		}
		//查找用户
		$memberData = Db::name('t_member')->where('phone',$phone)->find();
		if(!empty($memberData)){
			echo json_encode(array("success"=>0,"code"=>-1,"msg"=>"该手机已经注册"),true);
			return;
		}
		
		
    		$memberModel = new MemberModel();
		$result = $memberModel->createMember($instance);
		
		if($result["success"]){
			echo json_encode(array("success"=>1,"content"=>_unsetNull($result["instance"]),"code"=>200,"msg"=>"ok"),true);
		}else{
			echo json_encode(array("success"=>0,"code"=>1012,"msg"=>$result["error"]),true);
		}
	}	
	
	/**
	 *绑定账号
	 */
	function bind()
	{
		if(!$this->checkSignatrue()){return;}//检测签名
		
		$phone 		 = $this->getParam("phone");
		$checkCode	 = $this->getParam("checkCode");
		$openid		 = $this->getParam("openid");
		
		$instance = array("phone"=>$phone,"checkCode"=>$checkCode,"openid"=>$openid);
		if(!$this->pvalidate($instance,["phone","checkCode"],["req","req"],["手机","验证码"])){//校验
			return;
		}
		
		//查找用户
		$memberData = Db::name('t_member')->where('phone',$phone)->find();
		if(empty($memberData)){
			echo json_encode(array("success"=>0,"code"=>-1,"msg"=>"没有该手机用户"),true);
			return;
		}
		//校验验证码
    		$smsModel = new SmsModel();
		$isok = $smsModel->checkCode($phone,$checkCode);
		if(!$isok){
			echo json_encode(array("success"=>0,"code"=>-1,"msg"=>"验证码校验失败"),true);
			return;
		}
		
		//更新用户
		Db::name("t_member")->where("phone",$phone)->update(["openid"=>$openid]);
		$memberData = Db::name('t_member')->where('phone',$phone)->find();
		
		echo json_encode(array("success"=>1,"content"=>_unsetNull($memberData),"code"=>200,"msg"=>"ok"),true);
	}	
	/**
	 * 通过ID得到用户信息
	 */
	function getMemberByID()
	{
		if(!$this->checkSignatrue()){return;}//检测签名
		if(!$this->checkUserToken()){return;}//检测token
		
		$toMemberId 		 = $this->getParam("toMemberId");
		$instance = array("memberId"=>$toMemberId);
		if(!$this->pvalidate($instance,["memberId"],["req"],["会员ID"])){//校验
			return;
		}
		$memberIstance = Db::name("t_member")->where("id",$toMemberId)->find();
		
		if(!empty($memberIstance)){
			echo json_encode(array("success"=>1,"content"=>_unsetNull($memberIstance),"code"=>200,"msg"=>"ok"),true);
		}else{
			echo json_encode(array("success"=>0,"code"=>1013,"msg"=>"该ID对应会员为空"),true);
		}
	}	
	
	/**
	 * 通过openid得到用户信息
	 */
	function getMemberByOpenid()
	{
		if(!$this->checkSignatrue()){return;}//检测签名
		
		$openid 		 = $this->getParam("openid");
		if(!$this->pvalidate(["openid"=>$openid],["openid"],["req"],["openid"])){//校验
			return;
		}
		$memberIstance = Db::name("t_member")->where("openid",$openid)->find();
		
		if(!empty($memberIstance)){
			echo json_encode(array("success"=>1,"content"=>_unsetNull($memberIstance),"code"=>1,"msg"=>"ok"),true);
		}else{
			echo json_encode(array("success"=>0,"code"=>0,"msg"=>"该openid对应会员为空"),true);
		}
	}	


	/**
	 * 完善用户信息
	 */
	function updateMember()
	{
		if(!$this->checkSignatrue()){return;}//检测签名
		if(!$this->checkUserToken()){return;}//检测token
		
		$memberId 		 = $this->getParam("toMemberId");
		
		$memberInstance = array("realName"	=>$this->getParam("realName"),
								"province"	=>$this->getParam("province"),
								"city"		=>$this->getParam("city"),
								"age"		=>$this->getParam("age"),
								"sex"		=>$this->getParam("sex"),
								"birthday"	=>$this->getParam("birthday"),
								"email"		=>$this->getParam("email"));
		$memberInstance["id"]=$memberId;
		if(!$this->pvalidate($memberInstance,["id","age","sex"],["req,num>0","num>0","num"],["ID","年龄","性别"])){//校验
			return;
		}
		//删除ID
		unset($memberInstance["id"]);
		
		//$tarMember = $this->member_model->findById("t_member",$memberId);
		$tarMember = Db::name("t_member")->where("id",$memberId)->find();
		if(empty($tarMember)){
			echo json_encode(array("success"=>0,"code"=>1013,"msg"=>"该ID对应会员为空"),true);
			return;
		}
		Db::name("t_member")->where("id",$memberId)->update($memberInstance);
		//$this->member_model->updateById("t_member",$memberInstance,$memberId);
		$memberInstance = Db::name("t_member")->where("id",$memberId)->find();;
		
		echo json_encode(array("success"=>1,"content"=>_unsetNull($memberInstance),"code"=>200,"msg"=>"ok"),true);
	}

	/**粉丝管理*******************************************************************************************************/
	//我关注一个人
	function noticer(){
		if(!$this->checkSignatrue()){return;}//检测签名
		if(!$this->checkUserToken()){return;}//检测token
		
		$memberId 		 = $this->getParam("memberId");
		$noticerId 		 = $this->getParam("noticerId");
		
		if(!$this->pvalidate(array("noticerId"=>$noticerId),["noticerId"],["req,num"],["关注者ID"])){//校验
			return;
		}
		
		$data = Db::name("t_follow")->where("followId",$memberId)->where("memberId",$noticerId)->find();
		if(!empty($data)){
			echo json_encode(array("success"=>0,"code"=>-111,"msg"=>"该用户已经被您关注了，无需重复关注"),true);
			return;
		}
		
		Db::name('t_follow')->insert(["followId"=>$memberId,"memberId"=>$noticerId,"createTime"=>eeGetDateTime()]);

		echo json_encode(array("success"=>1,"code"=>200,"msg"=>"关注成功"),true);
	}
	
	//得到某个用户所有的关注者
	function getNoticer(){
		if(!$this->checkSignatrue()){return;}//检测签名
		
		
		$toMemberId 		 = $this->getParam("toMemberId");
		
		if(!$this->pvalidate(array("toMemberId"=>$toMemberId),["toMemberId"],["req,num"],["用户ID"])){//校验
			return;
		}
		
		$list = Db::query("select f.id as followId, m.id as memberId,m.account,m.realName,m.sex,m.avatar from t_follow f left join t_member m on f.memberId=m.id where f.followId=?",[$toMemberId]);
		return $list;
	}
	
	//删除我对某个会员的关注
	function deleteNoticer(){
		if(!$this->checkSignatrue()){return;}//检测签名
		if(!$this->checkUserToken()){return;}//检测token
		
		$memberId 		 = $this->getParam("memberId");
		$deleteMemberId 		 = $this->getParam("deleteMemberId");
		
		if(!$this->pvalidate(array("deleteMemberId"=>$deleteMemberId),["deleteMemberId"],["req,num"],["删除用户ID"])){//校验
			return;
		}
		
		$data = Db::name("t_follow")->where("followId",$memberId)->where("memberId",$deleteMemberId)->find();
		if(empty($data)){
			echo json_encode(array("success"=>0,"code"=>-112,"msg"=>"您还没有关注该用户"),true);
			return;
		}
		
		Db::name("t_follow")->where("followId",$memberId)->where("memberId",$deleteMemberId)->delete();
		echo json_encode(array("success"=>1,"code"=>200,"msg"=>"删除成功"),true);
	}
		
	
	
	/**
	 * 得到我的粉丝列表
	 */
	function myFollws()
	{
		if(!$this->checkSignatrue()){return;}//检测签名
		if(!$this->checkUserToken()){return;}//检测token
		
		$memberId 		 = $this->getParam("memberId");
    		$memberModel = new MemberModel();
			
		$flowsList = $memberModel->getFollws($memberId);
		
		echo json_encode(array("success"=>1,"content"=>_unsetNull($flowsList),"code"=>200,"msg"=>"ok"),true);
	}
//	
//	/**
//	 * 删除粉丝
//	 */
//	function deleteFollow()
//	{
//		if(!$this->checkSignatrue()){return;}//检测签名
//		if(!$this->checkUserToken()){return;}//检测token
//		
//		$memberId 		 = $this->getParam("memberId");
//		$followId 		 = $this->getParam("followId");
//  		$memberModel = new MemberModel();
//		$memberModel->deleteFollow($memberId,$followId);
//		
//		echo json_encode(array("success"=>1,"content"=>_unsetNull($followId),"code"=>200,"msg"=>"ok"),true);
//	}
//	
//	
}
